import axios from 'axios'
import ElementUI from 'element-ui';
// import QS from 'qs';
import router from '../router/index';
import { ElMessage } from './norepeat.js'
let Message = new ElMessage()
import $store from '@/vuex/store'
axios.defaults.withCredentials = true;  //允许携带cookie
if (process.env.NODE_ENV === 'development') {
    axios.defaults.baseURL = '/api'                  //开发地址用的是vue。config。js里面定义的代理    在本地调试 一般存在跨域问题所以在vue.config.js中定义
} else if (process.env.NODE_ENV === 'production') {
    var asd = "http://" + window.location.host + "/"
    axios.defaults.baseURL = asd         //线上请求地址
}
console.log(window.location)
console.log('当前接口连接地址：',axios.defaults.baseURL)

//设置超时10s
axios.defaults.timeout = 60000

// 请求头信息是为post请求设置
axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8'

axios.interceptors.response.use(
    response => {
        // 如果返回的状态码为200，说明接口请求成功，可以正常拿到数据
        // 否则的话抛出错误
        if (response.status === 200) {
            // console.log(response)
            return Promise.resolve(response);

        } else {
            return Promise.reject(response);
        }
    },
    error => {
        if(error.response.status){

            if(error.response.data.code == 20004){

                Message.error('登录失效，请重新登录!')
                $store.commit('setHasShow',false)
                router.push({path:'/login'})
            }else{
                Message.error(error.response.data.errMsg)
            }

            return Promise.reject(error.response)
        }
    }
);

/**
 * 请求get方法
 * */
export function get(url, params){
    return new Promise((resolve, reject) =>{
        axios.get(url+params)
        .then(res => {
            resolve(res.data);
        })
        .catch(err => {
            reject(err.data)
        })
    });
}

/**
 * 请求post方法
 */

export function post(url, params) {
    return new Promise((resolve, reject) => {
        axios.post(url, params)
        .then(res => {
            resolve(res.data);
        })
        .catch(err => {
            reject(err.data)
        })
    });
}

// form-data
export function postFormData(url, params) {
    return new Promise((resolve, reject) => {
        axios.post(url, params,{headers: {
            'Content-Type': 'multipart/form-data'
            } })
        .then(res => {
            resolve(res.data);
        })
        .catch(err => {
            reject(err.data)
        })
    });
}

export function getfile(url, params) {
    return new Promise((resolve, reject) => {
        axios.get(url, params,{headers: {
            responseType: 'blob',
            } })
        .then(res => {
            resolve(res);
        })
        .catch(err => {
            reject(err.data)
        })
    });
}

export function postfile(url, params) {
    return new Promise((resolve, reject) => {
        axios.post(url, params,{headers: {
            responseType: 'blob',
            } })
        .then(res => {
            resolve(res.data);
        })
        .catch(err => {
            reject(err.data)
        })
    });
}
